﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class Timingrules
    {
        private readonly Chain.IDAL.Timingrules dal = new Chain.IDAL.Timingrules();

        public int Add(Chain.Model.Timingrules model)
        {
            return this.dal.Add(model);
        }

        public List<Chain.Model.Timingrules> DataTableToList(DataTable dt)
        {
            List<Chain.Model.Timingrules> list = new List<Chain.Model.Timingrules>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.Timingrules item = new Chain.Model.Timingrules();
                    if ((dt.Rows[i]["RulesID"] != null) && (dt.Rows[i]["RulesID"].ToString() != ""))
                    {
                        item.RulesID = int.Parse(dt.Rows[i]["RulesID"].ToString());
                    }
                    if ((dt.Rows[i]["RulesName"] != null) && (dt.Rows[i]["RulesName"].ToString() != ""))
                    {
                        item.RulesName = dt.Rows[i]["RulesName"].ToString();
                    }
                    if ((dt.Rows[i]["RulesInterval"] != null) && (dt.Rows[i]["RulesInterval"].ToString() != ""))
                    {
                        item.RulesInterval = int.Parse(dt.Rows[i]["RulesInterval"].ToString());
                    }
                    if ((dt.Rows[i]["RulesUnitPrice"] != null) && (dt.Rows[i]["RulesUnitPrice"].ToString() != ""))
                    {
                        item.RulesUnitPrice = decimal.Parse(dt.Rows[i]["RulesUnitPrice"].ToString());
                    }
                    if ((dt.Rows[i]["RulesExceedTime"] != null) && (dt.Rows[i]["RulesExceedTime"].ToString() != ""))
                    {
                        item.RulesExceedTime = int.Parse(dt.Rows[i]["RulesExceedTime"].ToString());
                    }
                    if ((dt.Rows[i]["RulesAddTime"] != null) && (dt.Rows[i]["RulesAddTime"].ToString() != ""))
                    {
                        item.RulesAddTime = DateTime.Parse(dt.Rows[i]["RulesAddTime"].ToString());
                    }
                    if ((dt.Rows[i]["RulesShopID"] != null) && (dt.Rows[i]["RulesShopID"].ToString() != ""))
                    {
                        item.RulesShopID = int.Parse(dt.Rows[i]["RulesShopID"].ToString());
                    }
                    if ((dt.Rows[i]["RulesUserID"] != null) && (dt.Rows[i]["RulesUserID"].ToString() != ""))
                    {
                        item.RulesUserID = int.Parse(dt.Rows[i]["RulesUserID"].ToString());
                    }
                    if ((dt.Rows[i]["RulesRemark"] != null) && (dt.Rows[i]["RulesRemark"].ToString() != ""))
                    {
                        item.RulesRemark = dt.Rows[i]["RulesRemark"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int RulesID)
        {
            Chain.BLL.TimingProject project = new Chain.BLL.TimingProject();
            if (project.ExistsRules(RulesID))
            {
                return false;
            }
            Chain.BLL.OrderTime time = new Chain.BLL.OrderTime();
            return (!time.ExistsRules(RulesID) && this.dal.Delete(RulesID));
        }

        public bool DeleteList(string RulesIDlist)
        {
            return this.dal.DeleteList(RulesIDlist);
        }

        public bool Exists(int RulesID)
        {
            return this.dal.Exists(RulesID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, out resCount, strWhere);
        }

        public Chain.Model.Timingrules GetModel(int RulesID)
        {
            return this.dal.GetModel(RulesID);
        }

        public List<Chain.Model.Timingrules> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public bool Update(Chain.Model.Timingrules model)
        {
            return this.dal.Update(model);
        }
    }
}

